package com.example.shirotest.utils;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;

public class NetUtil {

    public static Document get(String url) throws IOException {
        URL realUrl = new URL(url);
        HttpURLConnection connection =(HttpURLConnection) realUrl.openConnection();
        connection.setRequestMethod("GET");
        connection.connect();
        BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
        String line;
        StringBuffer html = new StringBuffer();
        while ((line = reader.readLine())!= null){
            html.append(line);
        }

        //开始请求
        try {
            Document doc = Jsoup.parse(html.toString());
            System.out.println(doc);
            return doc;
        } catch (Exception e) {
            System.out.println("parse error: " + url);
            e.printStackTrace();
        }
        return null;
    }

}
